/**
 * Created by ljg on 2017/2/22.
 */
export default function () {
    angular.module('ui.table')
        .directive('uiTable', function (eventHelper) {
            return {
                restrict: 'E',
                replace: true,
                transclude: true,
                controller:function ($scope,$transclude) {

                    $scope.$on("ui.table.getData",function (event,data) {
                        $scope.datas=data;//table数据
                        $scope.handleAllCheck({currentTarget:{checked:false}});
                        $scope.$apply();
                    });
                    this._setHeads=function () {
                        $scope.heads=heads;
                    }

                },
                scope:{},
                require:"^?uiContainer",
                template: function(el,attr){
                    const dischecked=!attr.dischecked;
                    const attrKey=attr.key;
                    return      '<div class="table-responsive"><table class="table table-striped table-bordered table-hover">' +
                                    '<thead>' +
                                            '<tr>' +
                        (dischecked?'<th><input id={{"'+attrKey+'_check_table"}} type="checkbox" ng-click="handleAllCheck($event)"></th>':'') +
                                                '<th ng-repeat="c in columns">{{c.head}}</th>' +
                                                '<th>操作</th>'+
                                            '</tr>' +
                                    '</thead>' +
                                    '<tbody>' +
                                            '<tr ng-repeat="data in datas">' +
                        (dischecked?'<td><input id={{"'+attrKey+'_check_table_"+$index}}  type="checkbox" ng-click="handleCheck($event,data,\''+attrKey+'_check_table_\'+$index)"></td>':'')+
                                                '<td ng-repeat="c in columns">{{c.render?c.render(data,data[c.name]):data[c.name]}}</td>'+
                                                '<td><Button ng-repeat="ocs in operateColumns"  class="btn btn-sm btn-primary" ng-click="ocs.click(data)" >{{ocs.text}}</Button></td>' +
                                            '</tr>' +
                                    '</tbody>' +
                                '</table></div>';
                },
                link : function(scope, element, attrs,ctrl) {


                    const attrKey=attrs.key;
                    scope.columns =ctrl._getColumns();
                    scope.operateColumns=ctrl._getOperateColumns();
                    scope.selectKeys=[];
                    scope.selectItems=[];

                    scope.handleCheck=function (event,data,key) {
                        if(!event.currentTarget.checked){
                            //取消
                            scope.selectKeys.splice(scope.selectKeys.indexOf(key),1);
                            scope.selectItems.splice(scope.selectItems.indexOf(data),1);
                        }else {
                            //选取
                            scope.selectKeys.push(key);
                            scope.selectItems.push(data);
                        }

                        ctrl._selectedItems(scope.selectItems)
                    }
                    scope.handleAllCheck=function (event) {
                        if(!event.currentTarget.checked){
                            //取消
                            $("#"+attrKey+"_check_table").prop("checked",false);
                            scope.selectKeys.map(key=>{$("#"+key).prop("checked",false);})
                            scope.selectKeys=[];
                            scope.selectItems=[];
                        }else {
                            //选取
                            $("#"+attrKey+"_check_table").prop("checked",true);
                            scope.selectKeys=[];
                            scope.selectKeys=scope.datas.map((d,i)=>{
                                const key=attrKey+"_check_table_"+i;
                                $("#"+key).prop("checked",true);
                                return key;
                            });
                            scope.selectItems=[];

                            scope.selectItems=scope.datas.slice();
                        }
                        ctrl._selectedItems(scope.selectItems);
                    }
                }
            };
        });
}
